#!/usr/bin/env python3

def detect_email(section):
    tld_list = tld_list = [
        '.com',
        '.cn',
        '.org',
        '.edu',
        '.gov',
        '.uk',
        '.net',
        '.ca',
        '.de',
        '.jp',
        '.fr',
        '.au',
        '.us',
        '.ru',
        '.ch',
        '.it',
        '.nl',
        '.se',
        '.no',
        '.es',
        '.mil'
    ]
    working_string = section[0].lower()
    parsing = []

    if '.' not in working_string:
        return section, None
    if '@' not in working_string:
        return section, None
    for tld in tld_list:
        end_index = working_string.find(tld)
        if end_index != -1:
            end_index += len(tld)
            marker_index = working_string[0:end_index].find('@')
            if marker_index != -1:
                found = working_string[0:end_index]
                parsing.append((section[0][0:end_index],'email'))

                if end_index != len(working_string):
                    parsing.append((section[0][end_index:],None))

                return parsing, found

    return section, None

def email_detection(section_list):
    result = []
    index = 0
    while index < len(section_list):
        if section_list[index][1] == None:
            parsing, email= detect_email(section_list[index])
            if email:
                for w in parsing:
                    if w[1] == 'email':
                        back = '@'+w[0].split('@')[-1]
                        pre = w[0][:len(w[0])-len(back)]
                        if pre!='':
                            result.append((pre,None))
                        result.append((back,'email'))
                    else:
                        result.append(w)
            else:
                result.append(parsing)
        else:
            result.append(section_list[index])
        index += 1

    return result

if __name__ == '__main__':
    result = email_detection([("www.?@%.com",None)])
    print(result)
